package com.efuture.mutlithread;

/**
 * @ClassNAME ThreadStopTest
 * @Description 合理终止一个线程的做法
 * @Author zhaoshanshan
 * @Date 2021/9/10 12:21 上午
 * @Version 1.0
 */
public class ThreadStopTestTwo {
    public static void main(String[] args) {
        SonThreadStopTestTwo t = new SonThreadStopTestTwo();
        t.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.run = false;
    }
}

class SonThreadStopTestTwo extends Thread{
    boolean run = true;
    @Override
    public void run() {

            for (int i=0;i<10;i++){
                if(run){
                    System.out.println(i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    //这里可以做数据保存工作
                    return;
                }

            }
        }
}